package cn.ffcs.uec.reportParameter.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.common.vo.UecBdDefdoc;
import cn.ffcs.uec.organization.manager.UecOrgDeptManager;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.organization.model.UecOrgDept;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.reportParameter.action.bean.ReportParameterBean;
import cn.ffcs.uec.staffAndOutStaff.vo.StaffAndOutStaff;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.StrUtil;
/**
 * @desc 报表查询参数组件（人员花名册）
 * @author BMP
 *
 */
@Controller
@Scope("prototype")
public class ReportParameterComposer extends BasePortletComposer {

    private static final long serialVersionUID = -3660612675891201174L;

    private ReportParameterBean bean = new ReportParameterBean();

    private StaffAndOutStaff staffAndOutStaff = new StaffAndOutStaff();

    private UecOrgDeptManager uecOrgDeptManager = (UecOrgDeptManager) ApplicationContextUtil
            .getBean("uecOrgDeptManager");

    private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
            .getBean("uecOrgOrgsManager");

    /**
     * 操作类型
     */
    private String opType;

    @SuppressWarnings("unused")
    private UecBdDefdoc uecBdDefdoc;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        Components.wireVariables(comp, bean);
        this.bean.getNewPostLevel().addForward("onGetBdDefDoc", comp, "onGetBdDefDocResponse");
    }

    /**
     * 初始化窗口
     */
    public void onCreate$reportParameterWindow() {
        opType = (String) arg.get("opType");
        if ("confirm".equals(opType)) {
            this.bean.getReportParameterWindow().setTitle("报表参数赋值");
        }
    }

    /**
     * 监听新岗位体系
     */
    public void onGetBdPsnclResponse(final ForwardEvent event) {
        if (event.getOrigin().getData() != null) {
            uecBdDefdoc = (UecBdDefdoc) event.getOrigin().getData();
        }
    }

    /**
     * 确认
     * 
     * @throws Exception
     */
    public void onOk() throws Exception {
        if ("confirm".equals(opType)) {
            String msg = checkUacStaffData();
            if (msg != null) {
                Messagebox.show(msg);
                return;
            }
            // 公司--组织部门
            if (!StrUtil.isNullOrEmpty(bean.getUecOrganization().getValue())) {
                String orgType = bean.getUecOrganization().getOrganization().getOrgType();
              //组织主键
    			String pkorg = (String) bean.getUecOrganization().getOrganization().getOrg();
                if ("ORG".equals(orgType)) {
                    // 公司
                	String orgName=bean.getUecOrganization().getOrganization().getOrgName();
                	 if("中国电信股份有限公司内蒙古分公司".equals(orgName)) {
                		staffAndOutStaff
                		.setOrgName(null);
                	}else if(orgName.endsWith("*")){
                		staffAndOutStaff
			       		.setOrgName(orgName);
			       	}else {
			       		staffAndOutStaff.setPkOrg(pkorg);
			       		staffAndOutStaff.setOrgName(uecOrgOrgsManager
	                            .getUecOrgOrgsNameByPkOrg(pkorg));
			       	}
                } else {// 部门
                    // 部门主键
                    String pkdept = (String) bean.getUecOrganization().getOrganization().getOrg();
                    UecOrgDept uecOrgDept = new UecOrgDept();
                    uecOrgDept.setPkDept(pkdept);
                    uecOrgDept = uecOrgDeptManager.findUecOrgDeptByOrgDept(uecOrgDept);
                    staffAndOutStaff
                            .setDeptName(bean.getUecOrganization().getOrganization().getOrgName());
                    String orgName = uecOrgOrgsManager
                            .getUecOrgOrgsNameByPkOrg(uecOrgDept.getPkOrg());
                    staffAndOutStaff.setOrgName(orgName);
                	//UecOrgOrgs uecOrgOrgs= new UecOrgOrgs();
					//uecOrgOrgs.setPkOrg(uecOrgDept.getPkOrg());
					//uecOrgOrgs = uecOrgOrgsManager.findUecOrgOrgsByUecOrgOrgs(uecOrgOrgs);
					//String orgName=uecOrgOrgs.getName();
					staffAndOutStaff.setPkDept(uecOrgDept.getPkDept());
					staffAndOutStaff.setPkOrg(uecOrgDept.getPkOrg());
					//staffAndOutStaff.setOrgName(orgName);

                }
            }
            // 人员类别
            if (!StrUtil.isNullOrEmpty(bean.getUecPsncl().getValue())) {
                staffAndOutStaff.setUecBdPsnclList(bean.getUecPsncl().getUecBdPsncls());
            }
            // 截止日期
            if (!StrUtil.isNullOrEmpty(bean.getEndDate().getValue())) {
                String dateToStr = DateUtil.dateToStr(bean.getEndDate().getValue());
                staffAndOutStaff.setEndJobDate(dateToStr);
            }
            // 岗位层级(新)
            if (!StrUtil.isNullOrEmpty(bean.getNewPostLevel().getValue())) {
                staffAndOutStaff.setNewPostGradeList(bean.getNewPostLevel().getUecBdDefdocs());
            }
            // 岗位层级(旧)
            if (!StrUtil.isNullOrEmpty(bean.getOldPostLevel().getValue())) {
                staffAndOutStaff.setOldPostGradeList(bean.getOldPostLevel().getUecBdDefdocs());
            }
            // 岗位等级
            if (!StrUtil.isNullOrEmpty(bean.getPostGrade().getValue())) {
                staffAndOutStaff.setPostGradeList(bean.getPostGrade().getUecBdDefdocs());
            }
        }
        Events.sendEvent("onOK", this.self, staffAndOutStaff);
        this.onCancel();
    }

    /**
     * 校验
     * 
     * @return
     */
    private String checkUacStaffData() {
        if (StrUtil.isNullOrEmpty(bean.getUecOrganization().getValue())) {
            return "请选择公司";
        } else if (StrUtil.isNullOrEmpty(bean.getUecPsncl().getValue())) {
            return "请选择人员类别";
        } /*else if (StrUtil.isNullOrEmpty(bean.getEndDate().getValue())) {
            return "请选择截止日期";
        }*/
        return null;
    }

    /**
     * 取消
     */
    public void onCancel() {
        // 关闭窗口
        this.bean.getReportParameterWindow().onClose();
    }
}
